<?php
namespace Common\Model;

use Think\Model;


class WxTplmsgModel extends Model
{
    public function getWxTplMsg($condition = array(), $relation = false)
    {
        $data = $this->where($condition);
        if ($relation) {
            $data = $data->relation(true);
        }
        $data = $data->find();

        return $data;
    }

    public function getWxTplMsgList($condition = array(), $relation = false, $order = "id desc", $p = 0, $num = 0, $limit = 0)
    {
        $list = $this->where($condition);
        if ($relation) {
            $list = $list->relation(true);
        }
        if ($p && $num) {
            $list = $list->page($p . ',' . $num . '');
        }
        if ($limit) {
            $list = $list->limit($limit);
        }

        $list = $list->order($order)->select();

        return $list;
    }

    public function getWxTplMsgListCount($condition = array())
    {
        $count = $this->where($condition)->count();
        return $count;
    }

    public function clearWxTplmsg()
    {
        $this->where("1=1")->delete();
    }

    public function addWxTplMsg($data)
    {
        foreach ($data as $key => $value) {
            $tplmsg = $this->getWxTplMsg(array("template_id_short" => $key));
            if ($tplmsg) {
                $tplmsg["template_id"] = $value;
                $this->save($tplmsg);
            } else {
                $this->add(array("template_id_short" => $key, "template_id" => $value));
            }
        }

    }
}